﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;

namespace Loogn.WP.Notification
{
    public class WPNotificationPusher
    {
        private static Task<WPNotificationResult> PushNotification(string deviceURL, Dictionary<string, string> headers, byte[] data, string messageId)
        {
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(deviceURL);
            request.Method = WebRequestMethods.Http.Post;
            if (string.IsNullOrEmpty(messageId))
            {
                messageId = Guid.NewGuid().ToString();
            }
            request.Headers["X-MessageID"] = messageId;
            request.ContentType = "text/xml; charset=utf-8";
            if (headers != null)
            {
                foreach (var kv in headers)
                {
                    request.Headers.Add(kv.Key, kv.Value);
                }
            }

            request.ContentLength = data.Length;
            using (var stream = request.GetRequestStream())
            {
                stream.Write(data, 0, data.Length);
            }

            return Task.Run<WPNotificationResult>(() =>
            {
                WPNotificationResult result = new WPNotificationResult();
                try
                {
                    var response = (HttpWebResponse)request.GetResponse();
                    result.NotificationStatus = response.Headers["X-NotificationStatus"];
                    result.NotificationChannelStatus = response.Headers["X-SubscriptionStatus"];
                    result.DeviceConnectionStatus = response.Headers["X-DeviceConnectionStatus"];
                }
                catch (Exception exp)
                {
                    result.Error = exp;
                }
                return result;
            });
        }

        public static Task<WPNotificationResult> PushTileNotification(string deviceURL, WPTileMessage message, WPNotificationClass notificationClass, string messageId)
        {
            Dictionary<string, string> headers = new Dictionary<string, string>();
            switch (notificationClass)
            {
                case WPNotificationClass.Immediately:
                    headers.Add("X-NotificationClass", "1");
                    break;
                case WPNotificationClass.Within450:
                    headers.Add("X-NotificationClass", "11");
                    break;
                case WPNotificationClass.Within900:
                    headers.Add("X-NotificationClass", "21");
                    break;
                default:
                    break;
            }
            headers.Add("X-WindowsPhone-Target", "token");
            return PushNotification(deviceURL, headers, message.ToBytes(), messageId);
        }

        public static Task<WPNotificationResult> PushToastNotification(string deviceURL, WPToastMessage message, WPNotificationClass notificationClass, string messageId)
        {
            Dictionary<string, string> headers = new Dictionary<string, string>();
            switch (notificationClass)
            {
                case WPNotificationClass.Immediately:
                    headers.Add("X-NotificationClass", "2");
                    break;
                case WPNotificationClass.Within450:
                    headers.Add("X-NotificationClass", "12");
                    break;
                case WPNotificationClass.Within900:
                    headers.Add("X-NotificationClass", "22");
                    break;
                default:
                    break;
            }
            headers.Add("X-WindowsPhone-Target", "toast");
            return PushNotification(deviceURL, headers, message.ToBytes(), messageId);
        }

        public static Task<WPNotificationResult> PushRawNotification(string deviceURL, byte[] data, WPNotificationClass cls, string messageId)
        {
            Dictionary<string, string> headers = new Dictionary<string, string>();
            switch (cls)
            {
                case WPNotificationClass.Immediately:
                    headers.Add("X-NotificationClass", "3");
                    break;
                case WPNotificationClass.Within450:
                    headers.Add("X-NotificationClass", "13");
                    break;
                case WPNotificationClass.Within900:
                    headers.Add("X-NotificationClass", "23");
                    break;
                default:
                    break;
            }
            return PushNotification(deviceURL, headers,data, messageId);
        }
    }
}
